package com.troll.wanandroid.repo

import com.chad.library.adapter.base.entity.node.BaseNode
import com.rxlife.coroutine.RxLifeScope
import com.troll.eth.live.Live
import com.troll.wanandroid.model.SystemKnowledge
import com.troll.wanandroid.net.Response
import rxhttp.toClass
import rxhttp.wrapper.param.RxHttp

/**
 *
 * @Author:   TangPeng
 * @Time:     6/10/21
 * @Remark:  知识体系
 */
class SystemRepo : BaseRepo() {

    val liveKnowledgeTree = Live<MutableList<BaseNode>>()

    /**
     * 知识树数据
     */
    fun knowledgeTree(rxLifeScope: RxLifeScope) {
        val node = mutableListOf<BaseNode>()
        rxLifeScope.launch({
            val data = RxHttp.get("/tree/json")
                .toClass<Response<List<SystemKnowledge>>>()
                .await().data
                ?.forEach {
                    node.add(it)
                }
            liveKnowledgeTree.value = node

        }, onError = {
            liveError.value = true
        })
    }
}